package com.budwk.app.oss.config;

import cn.hutool.core.util.IdUtil;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.HttpMethodEnum;
import com.obs.services.model.TemporarySignatureRequest;
import com.obs.services.model.TemporarySignatureResponse;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: HweiOBSConfig
 * @Description: 华为云OBS配置类
 * @Author: dfsj
 * @Date: 2023-02-23 15:56
 * @Version: 1.0
 */
@Slf4j
@IocBean
public class HweiOBSConfig {
    @Inject
    private PropertiesProxy propertiesProxy;

    public static final String CONFIG_KEY = "huweiyun";

    /**
     * 访问密钥AK
     */
    public String getAccessKey() {
        return propertiesProxy.get("hwyun.obs.accessKey");
    }

    /**
     * 访问密钥SK
     */
    public String getSecurityKey() {
        return propertiesProxy.get("hwyun.obs.securityKey");
    }

    /**
     * 终端节点
     */
    public String getEndPoint() {
        return propertiesProxy.get("hwyun.obs.endPoint");
    }

    /**
     * 桶
     */
    public String getBucketName() {
        return propertiesProxy.get("hwyun.obs.bucketName");
    }

    /**
     * @Description 获取OBS客户端实例
     * @author dfsj
     * @date 2021/12/21 15:57
     * @return
     * @return: com.obs.services.ObsClient
     */
    public ObsClient getInstance() {
        return new ObsClient(getAccessKey(), getSecurityKey(), getEndPoint());
    }

    /**
     * @Description 销毁OBS客户端实例
     * @author dfsj
     * @date 2021/12/21 17:32
     * @param: obsClient
     * @return
     */
    public void destroy(ObsClient obsClient){
        try {
            obsClient.close();
        } catch (ObsException e) {
            log.error("obs执行失败", e);
        } catch (Exception e) {
            log.error("执行失败", e);
        }
    }

    public String getDownloadUrl(String fileName) {
        ObsClient obsClient = getInstance();
        // URL有效期，单位毫秒
        long expireSeconds = 599999999L;
        String objectName = fileName;
        TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
        request.setBucketName(getBucketName());
        request.setObjectKey(objectName);
        TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
        return response.getSignedUrl();
    }

    /**
     * 文件存放路径
     * @param suffix
     * @return
     */
    public static String getObjectKey(String suffix) {
        // 生成uuid
        String uuid = IdUtil.fastSimpleUUID();
        // 文件路径
        String path = new SimpleDateFormat("yyyy/MM/dd").format(new Date()) + "/" + uuid;
        return path + suffix;
    }

    /**
     * 文件存放路径
     * @param suffix
     * @param fileName
     * @return
     */
    public static String getObjectKey(String suffix, String fileName) {
        // 生成uuid
        String uuid = IdUtil.fastSimpleUUID();
        // 文件路径
        String path = getDatePath() + "/" + fileName + "-" + uuid;
        return path + suffix;
    }

    private static String getDatePath() {
        return new SimpleDateFormat("yyyy/MM/dd").format(new Date());
    }
}
